package com.fc.v2.controller;

import com.fc.v2.domain.AjaxResult;
import com.fc.v2.model.auto.SysRole;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.auto.ISysRoleService;
import com.fc.v2.service.auto.SysUserService;
import com.fc.v2.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 角色controller
 * @Classname SysRoleController
 * @Date 2022/1/25 19:40
 * @Created by 87766867@qq.com
 */
@RestController
@RequestMapping("/SysRoleController")
public class SysRoleController {

    private final ISysRoleService iSysRoleService;
    private final SysUserService sysUserService;
    public SysRoleController(ISysRoleService iSysRoleService,SysUserService sysUserService) {
        this.iSysRoleService = iSysRoleService;
        this.sysUserService=sysUserService;
    }

    /***
     * 分页查询
     * @params [tablepar]
     * @returns com.github.pagehelper.PageInfo<com.fc.v2.model.auto.SysRole>
     * @Date 2022/1/25 20:01
     * @Created by 87766867@qq.com
     */
    @GetMapping("/list")
    public PageInfo<SysRole> list(Tablepar tablepar){
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<SysRole> list= iSysRoleService.list();
        return new PageInfo<>(list);
    }



    /***
     * 根据角色id获取角色信息
     * @params [roleId]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/25 19:46
     * @Created by 87766867@qq.com
     */
    @GetMapping("/getRoleId/{roleId}")
    public AjaxResult getUserId(@PathVariable("roleId") String roleId){
        return AjaxResult.successData(iSysRoleService.getById(roleId));
    }

    /***
     * 添加角色
     * @params [sysRole]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/25 19:46
     * @Created by 87766867@qq.com
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysRole sysRole){
        sysRole.setId(SnowflakeIdWorker.getUUID());
        return AjaxResult.successData(iSysRoleService.save(sysRole));
    }
    /***
     * 修改用户
     * @params [sysRole]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/25 19:46
     * @Created by 87766867@qq.com
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysRole sysRole){
        return AjaxResult.successData(iSysRoleService.updateById(sysRole));
    }
    /***
     * 删除用户
     * @params [roleId]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/25 19:46
     * @Created by 87766867@qq.com
     */
    @DeleteMapping("/delete/{roleId}")
    public  AjaxResult delete(@PathVariable("roleId") String roleId){
        return AjaxResult.successData(iSysRoleService.removeById(roleId));
    }

    /**
     * 绑定角色
     * @params [userId, roles]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/26 9:53
     * @Created by 87766867@qq.com
     */
    @PostMapping("/userOnboundRole")
    public AjaxResult userBandRole(String userId,String roles){
        boolean b= iSysRoleService.userOnboundRole(userId,roles);
        if (b){
            return  AjaxResult.success();
        }else{
            return  AjaxResult.error();
        }
    }




}
